﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ConsumableRecordService
    {
        //增加一条数据
        public static bool Add(Model.ConsumableRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ConsumableRecord(");
            strSql.Append("cid,normal,bad,stock,date)");
            strSql.Append(" values (");
            strSql.Append("@cid,@normal,@bad,@stock,@date)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4),
					new SqlParameter("@normal", SqlDbType.Int,4),
					new SqlParameter("@bad", SqlDbType.Int,4),
					new SqlParameter("@stock", SqlDbType.Int,4),
					new SqlParameter("@date", SqlDbType.DateTime)};
            parameters[0].Value = model.cid;
            parameters[1].Value = model.normal;
            parameters[2].Value = model.bad;
            parameters[3].Value = model.stock;
            parameters[4].Value = model.date;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        //删除数据ByCID
        public static void DeleteByCID(int cid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ConsumableRecord ");
            strSql.Append(" where cid=@cid");
            SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4)
			};
            parameters[0].Value = cid;
            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        //删除一条数据ByID
        public static void Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ConsumableRecord ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        //更新一条数据
        public static bool Update(Model.ConsumableRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ConsumableRecord set ");
            strSql.Append("cid=@cid,");
            strSql.Append("normal=@normal,");
            strSql.Append("bad=@bad,");
            strSql.Append("stock=@stock,");
            strSql.Append("date=@date");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@cid", SqlDbType.Int,4),
					new SqlParameter("@normal", SqlDbType.Int,4),
					new SqlParameter("@bad", SqlDbType.Int,4),
					new SqlParameter("@stock", SqlDbType.Int,4),
					new SqlParameter("@date", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.cid;
            parameters[1].Value = model.normal;
            parameters[2].Value = model.bad;
            parameters[3].Value = model.stock;
            parameters[4].Value = model.date;
            parameters[5].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        //获取一条数据byID
        public static Model.ConsumableRecord SelectOneByID(int id)
        {
            string sql = string.Format("SELECT * FROM ConsumableRecord WHERE id = {0}",id);
            SqlDataReader dr = SQLHelper.ExecuteReader(SQLHelper.sqlConnectionString, CommandType.Text, sql);
            Model.ConsumableRecord m = new Model.ConsumableRecord();
            if (dr.HasRows)
            {
                m.id = int.Parse(dr[0].ToString());
                m.cid = int.Parse(dr[1].ToString());
                m.normal = int.Parse(dr[2].ToString());
                m.bad = int.Parse(dr[3].ToString());
                m.stock = int.Parse(dr[4].ToString());
                m.date = Convert.ToDateTime(dr[5]);
                m.consumable = ConsumableService.SelectOneByID(m.cid);
            }
            dr.Close();
            return m;
        }

        //获取列表
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ConsumableRecord.id,ConsumableRecord.normal,ConsumableRecord.bad,ConsumableRecord.stock,ConsumableRecord.date,Consumable.name ");
            strSql.Append("FROM ConsumableRecord,Consumable ");
            strSql.Append("WHERE ConsumableRecord.cid = Consumable.id");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" AND " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
    }
}
